#!/bin/bash

#bash <(echo "echo fuck");
DISTRO=$(cat /etc/*release  | grep "ID=" | grep -v "_ID=" | sed 's/^ID=//');
GITREPOURL="https://github.com/xenlism/minimalism.git";
GITTARURL="https://github.com/xenlism/minimalism/tarball/master";
TARGETURI="/tmp/xenlism-minimalism.tar.gz";

USRTHEMESDIR="/usr/share/themes";


echo "Welcome to Xenlism Minimalism Installation.";
echo "remark: ${DISTRO} linux";


function start {
	rm -rf /tmp/*minimalism*
	check_git
}
function check_git {
	if which git > /dev/null 
	then
		download_with_git
	else
		install_git
	fi
}
function install_pkg {
	cd /tmp;
	SOURCEDIR=$(ls -a /tmp | grep minimalism | grep -v "tar.gz")
	SOURCEDIR=$(echo "/tmp/${SOURCEDIR}");
	if [ -d $SOURCEDIR ]
	then
		cd "${SOURCEDIR}";
		if [ -d "${SOURCEDIR}/themes/" ]
		then
			echo "Info: Install Gnome / GTK Theme."
			cd "${SOURCEDIR}/themes/"
  			sudo cp -r * "${USRTHEMESDIR}";
  			sudo find "${USRTHEMESDIR}" -type d -exec chmod 755 '{}' \;
  			sudo find "${USRTHEMESDIR}" -type f -exec chmod 644 '{}' \;
		else
			echo "Error; No Gnome / GTK theme source found."
		
		fi
	else 
		echo "Error: No Source Found"
		echo "No source directory.Please try again later."
	fi
}

function unpack_tar {
	if [ -f $TARGETURI ]
	then
		tar -zxvf $TARGETURI -C /tmp;
		install_pkg
	else
		echo "Error: Download error."
		echo "No tar Download file exits.Please try again later."
	fi
}
function download_with_git {
	if which git > /dev/null
	then
		cd /tmp
		git clone "${GITREPOURL}"
		install_pkg
	else 	
		echo "Error: not found curl and wget."
		echo "You have to install wget or curl first and try again."
	fi
}
function download_with_curl_wget {
	echo "Info: Checking curl and wget."
	if which wget > /dev/null
	then
		echo "Info: Installation found wget."
		echo "Download source file from github."
		wget $GITTARURL -O $TARGETURI;
		tar -zxvf $TARGETURI -C /tmp;
		install_pkg
	elif which curl > /dev/null
	then
		echo "Info: Installation found wget."
		echo "Download source file from github."
		curl $GITTARURL -o $TARGETURI;
		tar -zxvf $TARGETURI -C /tmp;
		install_pkg
	else 
		echo "Error:Your System No git wget and curl."
		echo "You have to install git , wget or curl and try again later."
	fi
}
function install_git {
	if which apt-get > /dev/null
	then
		sudo apt-get update;
		sudo apt-get upgrade;
		sudo apt-get install git;
		start
	elif which pacman > /dev/null
	then
		sudo pacman -Syyu;
		sudo pacman -S git;
		start
	elif which yum > /dev/null
	then
		sudo yum -y update;
		sudo yum -y install git;
		start
	elif which dnf > /dev/null
	then
		sudo dnf update;
		sudo dnf install git;
		start
	elif which zypper > /dev/null
	then
		sudo zypper refresh;
		sudo zypper update;
		sudo zypper install git;
		start
	elif which urpmi > /dev/null
	then
		sudo urpmi.update -a 
		sudo urpmi git 
		start
	else
		echo "Info: Your linux distro not support."
		echo "Installer have to download source with curl or wget."
		download_with_curl_wget
	fi
}
start

